#include "atcommond.h"
#include "myserialwidget.h"


AtCommond::AtCommond(AT_CMD_TYPE id, MySerialWidget *serial,void *ctx):
    id(id),
    ctx(ctx),
    serial(serial)
{

}

AtCommond::AT_CMD_STATUS AtCommond::sendAndRecv(void **out, void *ctx1)
{
    QByteArray sendbuf = create_request_data(ctx1);

    recvbuf.clear();
    recvlen = -1;

    serial->syncSendAndRecv(sendbuf, recvbuf, recvlen, nullptr);

    if (!check_response_valid()) {
        return CMD_FAIL;
    }

    parse_response_data(out, ctx1);

    return CMD_OK;
}

bool AtCommond::check_response_valid()
{
    QString recv_string(recvbuf);
    recv_string = recv_string.toUpper();
    if (recv_string.contains("OK")) {
        return true;
    } else if (recv_string.contains("ERROR")) {
        return true;
    }
    return false;
//    if (env->contains(env, "OK")) {
//        //+CSQ:<rssi>,<ber>
//        if ((p = env->contains(env, "+CSQ")) != NULL &&
//            sscanf(p, "+CSQ:%d,%d", &rssi, &ber) == 2) {
//            printf("The CSQ read ok, rssi:%d, ber:%d\r\n", rssi, ber);
//        }
//        return true;
//    } else if (env->contains(env, "ERROR")) {  //错误处理
//        printf("The CSQ read failed.\r\n");
//        return true;
//    } else if (env->is_timeout(env, 1000)) {   //错误处理
//        printf("The CSQ read timeout.\r\n");
//        return true;
//    }
}

QueryLidarResolutionCommond::QueryLidarResolutionCommond(MySerialWidget *serial, void *ctx):
    AtCommond(QUERY_LIDAR_RESOLUTION, serial, ctx)
{

}

QByteArray QueryLidarResolutionCommond::create_request_data(void *ctx1)
{
    Q_UNUSED(ctx1);

    return QString("AT+QUERY_LIDAR_RESOLUTION").toUtf8();
}

int QueryLidarResolutionCommond::parse_response_data(void **out, void *ctx1)
{
    Q_UNUSED(ctx1);

    result.width = 64;
    result.height = 8;

    *out = &result;

    return CMD_OK;
}
